﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using Enities;

namespace QLThuVien_Nhom21
{
    public partial class QLTaiLieu : Form
    {

        public QLTaiLieu()
        {
            InitializeComponent();
        }

        LuuTruTaiLieu tailieu = new LuuTruTaiLieu();
        public void xoatrang()
        {

            txttentailieu.Text = "";
            txtmatailieu.Text = "";
            cbbtheloai.Text = "";
            cbbchuyenngah.Text = "";
            txtmatacgia.Text = "";
            txtnxuanban.Text = "";
            txtlantaiban.Text = "";
            txtmota.Text = "";
            txtgiabia.Text = "";
            txtsotrang.Text = ""; ;
            cbbvitriluutru.Text = "";
            cbbtrangthai.Text = "";
            cbbtinhtrang.Text = "";
        }

        private void bttthem_Click(object sender, EventArgs e)
        {

            if (txtmatailieu.Text == "" || txttentailieu.Text == "" || cbbtheloai.Text == "" || cbbchuyenngah.Text == "")
            {
                MessageBox.Show("Yêu cầu nhập đầy đủ thông tin", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            try
            {
                TaiLieu tl = new TaiLieu();

                tl.MaTaiLieu = txtmatailieu.Text;
                tl.TenTaiLieu = txttentailieu.Text;
                tl.TheLoai = cbbtheloai.Text;
                tl.ChuyenNganh = cbbchuyenngah.Text;
                tl.TacGia = txtmatacgia.Text;
                tl.NhaXuatBan = txtnxuanban.Text;
                if (txtlantaiban.Text.Length != 0)
                {
                    tl.LanTaiBan = int.Parse(txtlantaiban.Text);
                }
                tl.MoTa = txtmota.Text;
                if (txtgiabia.Text.Length != 0)
                {
                    tl.GiaBia = int.Parse(txtgiabia.Text);
                }
                if (txtsotrang.Text.Length != 0)
                {
                    tl.SoTrang = int.Parse(txtsotrang.Text);
                }
                tl.MaLuuTru = cbbvitriluutru.Text;
                tl.NgayNhap = dtngaynhap.Value;
                tl.TrangThai = cbbtrangthai.Text;
                tl.TinhTrang = cbbtinhtrang.Text;
                tailieu.ThemTL(tl);
                xoatrang();
                dtwDSQL.DataSource = tailieu.Laydanhsach();
            }
            catch
            { MessageBox.Show("Bạn chỉ được nhập số", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); }

        }

        private void bttdanhsachQL_Click_1(object sender, EventArgs e)
        {
            dtwDSQL.DataSource = tailieu.Laydanhsach();

        }

        private void bttsua_Click(object sender, EventArgs e)
        {
            if (txtmatailieu.Text == "")
            {
                MessageBox.Show("Nhập vào Mã tài liệu cần sửa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            else
            {
                TaiLieu tl = new TaiLieu();
                tl.MaTaiLieu = txtmatailieu.Text;
                tl.TenTaiLieu = txttentailieu.Text;
                tl.TheLoai = cbbtheloai.Text;
                tl.ChuyenNganh = cbbchuyenngah.Text;
                tl.TacGia = txtmatacgia.Text;
                tl.NhaXuatBan = txtnxuanban.Text;
                if (txtlantaiban.Text.Length != 0)
                {
                    tl.LanTaiBan = int.Parse(txtlantaiban.Text);
                }
                tl.MoTa = txtmota.Text;
                if (txtgiabia.Text.Length != 0)
                {
                    tl.GiaBia = int.Parse(txtgiabia.Text);
                }
                if (txtsotrang.Text.Length != 0)
                {
                    tl.SoTrang = int.Parse(txtsotrang.Text);
                }
                tl.MaLuuTru = cbbvitriluutru.Text;
                tl.NgayNhap = dtngaynhap.Value;
                tl.TrangThai = cbbtrangthai.Text;
                tl.TinhTrang = cbbtinhtrang.Text;
                tailieu.SuaTL(tl);
                xoatrang();
            }
            dtwDSQL.DataSource = tailieu.Laydanhsach();
        }

        private void bttxoa_Click(object sender, EventArgs e)
        {
            if (txtmatailieu.Text == "")
            {
                MessageBox.Show("Nhập vào mã tài liệu cần xóa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                tailieu.XoaTL(txtmatailieu.Text);
                txtmatailieu.Text = "";
            }
            dtwDSQL.DataSource = tailieu.Laydanhsach();
        }


        private void btthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btttimkiemDG_Click(object sender, EventArgs e)
        {
            if (txtmatlTKDG.Text == "" || txttentlTKDG.Text == "")
            {
                MessageBox.Show("Bạn nhập vào từ khóa tìm kiếm", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                dtgwTKDG.DataSource = tailieu.timkiemdongian(txtmatlTKDG.Text, txttentlTKDG.Text);

            }

        }

        private void bttthoatDG_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btttimkiemNC_Click(object sender, EventArgs e)
        {
            if (txtmatlNC.Text == "" || txttentlNC.Text == "" || txtNXBNC.Text == "" ||
                cbbtheloaiNC.Text == "" || txttacgiaNC.Text == "")
            {
                MessageBox.Show("Bạn nhập vào từ khóa tìm kiếm", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                dtwTKNC.DataSource = tailieu.timkiemnangcao(txtmatlNC.Text, txttentlNC.Text, txtNXBNC.Text,
                cbbtheloaiNC.Text, txttacgiaNC.Text);
            }
        }


        private void bttthoatNC_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void QLTaiLieu_Load(object sender, EventArgs e)
        {
            dtwDSQL.DataSource = tailieu.Laydanhsach();

        }

        private void txttentailieu_TextChanged(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void dtwDSQL_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            txtmatailieu.Text = dtwDSQL.Rows[e.RowIndex].Cells[0].Value + "";

            txttentailieu.Text = dtwDSQL.Rows[e.RowIndex].Cells[1].Value + "";
            cbbtheloai.Text = dtwDSQL.Rows[e.RowIndex].Cells[2].Value + "";
            cbbchuyenngah.Text = dtwDSQL.Rows[e.RowIndex].Cells[3].Value + "";
            txtmatacgia.Text = dtwDSQL.Rows[e.RowIndex].Cells[4].Value + "";
            txtnxuanban.Text = dtwDSQL.Rows[e.RowIndex].Cells[5].Value + "";
            txtlantaiban.Text = dtwDSQL.Rows[e.RowIndex].Cells[6].Value + "";

            txtmota.Text = dtwDSQL.Rows[e.RowIndex].Cells[7].Value + "";


            txtgiabia.Text = dtwDSQL.Rows[e.RowIndex].Cells[8].Value + "";

            txtsotrang.Text = dtwDSQL.Rows[e.RowIndex].Cells[9].Value + "";

            cbbvitriluutru.Text = dtwDSQL.Rows[e.RowIndex].Cells[10].Value + "";
            //dtngaynhap.Value = dtwDSQL.Rows[e.RowIndex].Cells[0].Value + "";
            cbbtrangthai.Text = dtwDSQL.Rows[e.RowIndex].Cells[11].Value + "";
            cbbtinhtrang.Text = dtwDSQL.Rows[e.RowIndex].Cells[12].Value + "";

        }

      
       
    }
}
